home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / w3 / font.el.z / font.el
Encoding:
Text File  |  1998-05-21  |  42.0 KB  |  1,252 lines

  1. ;;; font.el --- New font model
  2. ;; Author: wmperry
  3. ;; Created: 1997/12/24 16:32:55
  4. ;; Version: 1.55
  5. ;; Keywords: faces
  6.  
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;;; Copyright (c) 1995, 1996 by William M. Perry <wmperry@cs.indiana.edu>
  9. ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
  10. ;;;
  11. ;;; This file is part of GNU Emacs.
  12. ;;;
  13. ;;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;;; it under the terms of the GNU General Public License as published by
  15. ;;; the Free Software Foundation; either version 2, or (at your option)
  16. ;;; any later version.
  17. ;;;
  18. ;;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;;; GNU General Public License for more details.
  22. ;;;
  23. ;;; You should have received a copy of the GNU General Public License
  24. ;;; along with GNU Emacs; see the file COPYING.  If not, write to the
  25. ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  26. ;;; Boston, MA 02111-1307, USA.
  27. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  28.  
  29. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  30. ;;; The emacsen compatibility package - load it up before anything else
  31. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  32. (require 'cl)
  33. (require 'devices)
  34.  
  35. ;; Needed for XEmacs 19.13, noop on all others, since it is always loaded.
  36. (require 'disp-table)
  37.  
  38. (eval-and-compile
  39.   (condition-case ()
  40.       (require 'custom)
  41.     (error nil))
  42.   (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
  43.       nil ;; We've got what we needed
  44.     ;; We have the old custom-library, hack around it!
  45.     (defmacro defgroup (&rest args)
  46.       nil)
  47.     (defmacro defcustom (var value doc &rest args) 
  48.       (` (defvar (, var) (, value) (, doc))))))
  49.  
  50. (if (not (fboundp 'try-font-name))
  51.     (defun try-font-name (fontname &rest args)
  52.       (case window-system
  53.     ((x win32 w32 pm) (car-safe (x-list-fonts fontname)))
  54.     (ns (car-safe (ns-list-fonts fontname)))
  55.     (otherwise nil))))
  56.  
  57. (if (not (fboundp 'facep))
  58.     (defun facep (face)
  59.       "Return t if X is a face name or an internal face vector."
  60.       (if (not window-system)
  61.       nil                ; FIXME if FSF ever does TTY faces
  62.     (and (or (internal-facep face)
  63.          (and (symbolp face) (assq face global-face-data)))
  64.          t))))
  65.  
  66. (if (not (fboundp 'set-face-property))
  67.     (defun set-face-property (face property value &optional locale
  68.                    tag-set how-to-add)
  69.       "Change a property of FACE."
  70.       (and (symbolp face)
  71.        (put face property value))))
  72.  
  73. (if (not (fboundp 'face-property))
  74.     (defun face-property (face property &optional locale tag-set exact-p)
  75.       "Return FACE's value of the given PROPERTY."
  76.       (and (symbolp face) (get face property))))
  77.  
  78. (require 'disp-table)
  79.  
  80. (if (not (fboundp '<<))   (fset '<< 'lsh))
  81. (if (not (fboundp '&))    (fset '& 'logand))
  82. (if (not (fboundp '|))    (fset '| 'logior))
  83. (if (not (fboundp '~))    (fset '~ 'lognot))
  84. (if (not (fboundp '>>))   (defun >> (value count) (<< value (- count))))
  85.  
  86.  
  87. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  88. ;;; Lots of variables / keywords for use later in the program
  89. ;;; Not much should need to be modified
  90. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  91. (defconst font-running-xemacs (string-match "XEmacs" (emacs-version))
  92.   "Whether we are running in XEmacs or not.")
  93.  
  94. (defmacro define-font-keywords (&rest keys)
  95.   (`
  96.    (eval-and-compile
  97.      (let ((keywords (quote (, keys))))
  98.        (while keywords
  99.      (or (boundp (car keywords))
  100.          (set (car keywords) (car keywords)))
  101.      (setq keywords (cdr keywords)))))))  
  102.  
  103. (defconst font-window-system-mappings
  104.   '((x        . (x-font-create-name x-font-create-object))
  105.     (ns       . (ns-font-create-name ns-font-create-object))
  106.     (win32    . (x-font-create-name x-font-create-object))
  107.     (w32      . (x-font-create-name x-font-create-object))
  108.     (pm       . (x-font-create-name x-font-create-object)) ; Change? FIXME
  109.     (tty      . (tty-font-create-plist tty-font-create-object)))
  110.   "An assoc list mapping device types to the function used to create
  111. a font name from a font structure.")
  112.  
  113. (defconst ns-font-weight-mappings
  114.   '((:extra-light . "extralight")
  115.     (:light       . "light")
  116.     (:demi-light  . "demilight")
  117.     (:medium      . "medium")
  118.     (:normal      . "medium")
  119.     (:demi-bold   . "demibold")
  120.     (:bold        . "bold")
  121.     (:extra-bold  . "extrabold"))
  122.   "An assoc list mapping keywords to actual NeXTstep specific
  123. information to use")
  124.  
  125. (defconst x-font-weight-mappings
  126.   '((:extra-light . "extralight")
  127.     (:light       . "light")
  128.     (:demi-light  . "demilight")
  129.     (:demi        . "demi")
  130.     (:book        . "book")
  131.     (:medium      . "medium")
  132.     (:normal      . "medium")
  133.     (:demi-bold   . "demibold")
  134.     (:bold        . "bold")
  135.     (:extra-bold  . "extrabold"))
  136.   "An assoc list mapping keywords to actual Xwindow specific strings
  137. for use in the 'weight' field of an X font string.")
  138.  
  139. (defconst font-possible-weights
  140.   (mapcar 'car x-font-weight-mappings))
  141.  
  142. (defvar font-rgb-file nil
  143.   "Where the RGB file was found.")
  144.  
  145. (defvar font-maximum-slippage "1pt"
  146.   "How much a font is allowed to vary from the desired size.")
  147.  
  148. (defvar font-family-mappings
  149.   '(
  150.     ("serif"        . ("new century schoolbook"
  151.                "utopia"
  152.                "charter"
  153.                "times"
  154.                "lucidabright"
  155.                "garamond"
  156.                "palatino"
  157.                "times new roman"
  158.                "baskerville"
  159.                "bookman"
  160.                "bodoni"
  161.                "computer modern"
  162.                "rockwell"
  163.                ))
  164.     ("sans-serif"   . ("lucida"
  165.                "helvetica"
  166.                "gills-sans"
  167.                "avant-garde"
  168.                "univers"
  169.                "optima"))
  170.     ("elfin"        . ("tymes"))
  171.     ("monospace"    . ("courier"
  172.                "courier new"
  173.                "fixed"
  174.                "lucidatypewriter"
  175.                "clean"
  176.                "terminal"))
  177.     ("cursive"      . ("sirene"
  178.                "zapf chancery"))
  179.     )
  180.   "A list of font family mappings.")
  181.  
  182. (define-font-keywords :family :style :size :registry :encoding)
  183.  
  184. (define-font-keywords
  185.   :weight :extra-light :light :demi-light :medium :normal :demi-bold
  186.   :bold :extra-bold)
  187.  
  188. (defvar font-style-keywords nil)
  189.  
  190. (defsubst set-font-family (fontobj family)
  191.   (aset fontobj 1 family))
  192.  
  193. (defsubst set-font-weight (fontobj weight)
  194.   (aset fontobj 3 weight))
  195.  
  196. (defsubst set-font-style (fontobj style)
  197.   (aset fontobj 5 style))
  198.  
  199. (defsubst set-font-size (fontobj size)
  200.   (aset fontobj 7 size))
  201.  
  202. (defsubst set-font-registry (fontobj reg)
  203.   (aset fontobj 9 reg))
  204.  
  205. (defsubst set-font-encoding (fontobj enc)
  206.   (aset fontobj 11 enc))
  207.  
  208. (defsubst font-family (fontobj)
  209.   (aref fontobj 1))
  210.  
  211. (defsubst font-weight (fontobj)
  212.   (aref fontobj 3))
  213.  
  214. (defsubst font-style (fontobj)
  215.   (aref fontobj 5))
  216.  
  217. (defsubst font-size (fontobj)
  218.   (aref fontobj 7))
  219.  
  220. (defsubst font-registry (fontobj)
  221.   (aref fontobj 9))
  222.  
  223. (defsubst font-encoding (fontobj)
  224.   (aref fontobj 11))
  225.  
  226. (eval-when-compile
  227.   (defmacro define-new-mask (attr mask)
  228.     (`
  229.      (progn
  230.        (setq font-style-keywords
  231.          (cons (cons (quote (, attr))
  232.              (cons
  233.               (quote (, (intern (format "set-font-%s-p" attr))))
  234.               (quote (, (intern (format "font-%s-p" attr))))))
  235.            font-style-keywords))
  236.        (defconst (, (intern (format "font-%s-mask" attr))) (<< 1 (, mask))
  237.      (, (format
  238.          "Bitmask for whether a font is to be rendered in %s or not."
  239.          attr)))
  240.        (defun (, (intern (format "font-%s-p" attr))) (fontobj)
  241.      (, (format "Whether FONTOBJ will be renderd in `%s' or not." attr))
  242.      (if (/= 0 (& (font-style fontobj)
  243.               (, (intern (format "font-%s-mask" attr)))))
  244.          t
  245.        nil))
  246.        (defun (, (intern (format "set-font-%s-p" attr))) (fontobj val)
  247.      (, (format "Set whether FONTOBJ will be renderd in `%s' or not."
  248.             attr))
  249.      (cond
  250.       (val
  251.        (set-font-style fontobj (| (font-style fontobj)
  252.                       (, (intern
  253.                       (format "font-%s-mask" attr))))))
  254.       (((, (intern (format "font-%s-p" attr))) fontobj)
  255.        (set-font-style fontobj (- (font-style fontobj)
  256.                       (, (intern
  257.                       (format "font-%s-mask" attr))))))))
  258.        ))))
  259.  
  260. (let ((mask 0))
  261.   (define-new-mask bold        (setq mask (1+ mask)))
  262.   (define-new-mask italic      (setq mask (1+ mask)))
  263.   (define-new-mask oblique     (setq mask (1+ mask)))
  264.   (define-new-mask dim         (setq mask (1+ mask)))
  265.   (define-new-mask underline   (setq mask (1+ mask)))
  266.   (define-new-mask overline    (setq mask (1+ mask)))
  267.   (define-new-mask linethrough (setq mask (1+ mask)))
  268.   (define-new-mask strikethru  (setq mask (1+ mask)))
  269.   (define-new-mask reverse     (setq mask (1+ mask)))
  270.   (define-new-mask blink       (setq mask (1+ mask)))
  271.   (define-new-mask smallcaps   (setq mask (1+ mask)))
  272.   (define-new-mask bigcaps     (setq mask (1+ mask)))
  273.   (define-new-mask dropcaps    (setq mask (1+ mask))))
  274.  
  275. (defvar font-caps-display-table
  276.   (let ((table (make-display-table))
  277.     (i 0))
  278.     ;; Standard ASCII characters
  279.     (while (< i 26)
  280.       (aset table (+ i ?a) (+ i ?A))
  281.       (setq i (1+ i)))
  282.     ;; Now ISO translations
  283.     (setq i 224)
  284.     (while (< i 247)            ;; Agrave - Ouml
  285.       (aset table i (- i 32))
  286.       (setq i (1+ i)))
  287.     (setq i 248)
  288.     (while (< i 255)            ;; Oslash - Thorn
  289.       (aset table i (- i 32))
  290.       (setq i (1+ i)))
  291.     table))    
  292.  
  293. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  294. ;;; Utility functions
  295. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  296. (defsubst set-font-style-by-keywords (fontobj styles)
  297.   (make-local-variable 'font-func)
  298.   (declare (special font-func))
  299.   (if (listp styles)
  300.       (while styles
  301.     (setq font-func (car-safe (cdr-safe (assq (car styles) font-style-keywords)))
  302.           styles (cdr styles))
  303.     (and (fboundp font-func) (funcall font-func fontobj t)))
  304.     (setq font-func (car-safe (cdr-safe (assq styles font-style-keywords))))
  305.     (and (fboundp font-func) (funcall font-func fontobj t))))
  306.  
  307. (defsubst font-properties-from-style (fontobj)
  308.   (let ((style (font-style fontobj))
  309.     (todo font-style-keywords)
  310.     type func retval)
  311.     (while todo
  312.       (setq func (cdr (cdr (car todo)))
  313.         type (car (pop todo)))
  314.       (if (funcall func fontobj)
  315.       (setq retval (cons type retval))))
  316.     retval))
  317.  
  318. (defun font-unique (list)
  319.   (let ((retval)
  320.     (cur))
  321.     (while list
  322.       (setq cur (car list)
  323.         list (cdr list))
  324.       (if (member cur retval)
  325.       nil
  326.     (setq retval (cons cur retval))))
  327.     (nreverse retval)))
  328.  
  329. (defun font-higher-weight (w1 w2)
  330.   (let ((index1 (length (memq w1 font-possible-weights)))
  331.     (index2 (length (memq w2 font-possible-weights))))
  332.     (cond
  333.      ((<= index1 index2)
  334.       (or w1 w2))
  335.      ((not w2)
  336.       w1)
  337.      (t
  338.       w2))))
  339.  
  340. (defun font-spatial-to-canonical (spec &optional device)
  341.   "Convert SPEC (in inches, millimeters, points, or picas) into points"
  342.   ;; 1 in = 6 pa = 25.4 mm = 72 pt
  343.   (cond
  344.    ((numberp spec)
  345.     spec)
  346.    ((null spec)
  347.     nil)
  348.    (t
  349.     (let ((num nil)
  350.       (type nil)
  351.       ;; If for any reason we get null for any of this, default
  352.       ;; to 1024x768 resolution on a 17" screen
  353.       (pix-width (float (or (device-pixel-width device) 1024)))
  354.       (mm-width (float (or (device-mm-width device) 293)))
  355.       (retval nil))
  356.       (cond
  357.        ((string-match "^ *\\([-+*/]\\) *" spec) ; math!  whee!
  358.     (let ((math-func (intern (match-string 1 spec)))
  359.           (other (font-spatial-to-canonical
  360.               (substring spec (match-end 0) nil)))
  361.           (default (font-spatial-to-canonical
  362.             (font-default-size-for-device device))))
  363.       (if (fboundp math-func)
  364.           (setq type "px"
  365.             spec (int-to-string (funcall math-func default other)))
  366.         (setq type "px"
  367.           spec (int-to-string other)))))
  368.        ((string-match "[^0-9.]+$" spec)
  369.     (setq type (substring spec (match-beginning 0))
  370.           spec (substring spec 0 (match-beginning 0))))
  371.        (t
  372.     (setq type "px"
  373.           spec spec)))
  374.       (setq num (string-to-number spec))
  375.       (cond
  376.        ((member type '("pixel" "px" "pix"))
  377.     (setq retval (* num (/ pix-width mm-width) (/ 25.4 72.0))))
  378.        ((member type '("point" "pt"))
  379.     (setq retval num))
  380.        ((member type '("pica" "pa"))
  381.     (setq retval (* num 12.0)))
  382.        ((member type '("inch" "in"))
  383.     (setq retval (* num 72.0)))
  384.        ((string= type "mm")
  385.     (setq retval (* num (/ 72.0 25.4))))
  386.        ((string= type "cm")
  387.     (setq retval (* num 10 (/ 72.0 25.4))))
  388.        (t
  389.     (setq retval num))
  390.        )
  391.       retval))))
  392.  
  393.  
  394. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  395. ;;; The main interface routines - constructors and accessor functions
  396. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  397. (defun make-font (&rest args)
  398.   (vector :family
  399.       (if (stringp (plist-get args :family))
  400.           (list (plist-get args :family))
  401.         (plist-get args :family))
  402.       :weight
  403.       (plist-get args :weight)
  404.       :style
  405.       (if (numberp (plist-get args :style))
  406.           (plist-get args :style)
  407.         0)
  408.       :size
  409.       (plist-get args :size)
  410.       :registry
  411.       (plist-get args :registry)
  412.       :encoding
  413.       (plist-get args :encoding)))
  414.  
  415. (defun font-create-name (fontobj &optional device)
  416.   (let* ((type (device-type device))
  417.      (func (car (cdr-safe (assq type font-window-system-mappings)))))
  418.     (and func (fboundp func) (funcall func fontobj device))))
  419.  
  420. ;;;###autoload
  421. (defun font-create-object (fontname &optional device)
  422.   (let* ((type (device-type device))
  423.      (func (car (cdr (cdr-safe (assq type font-window-system-mappings))))))
  424.     (and func (fboundp func) (funcall func fontname device))))
  425.  
  426. (defun font-combine-fonts-internal (fontobj-1 fontobj-2)
  427.   (let ((retval (make-font))
  428.     (size-1 (and (font-size fontobj-1)
  429.              (font-spatial-to-canonical (font-size fontobj-1))))
  430.     (size-2 (and (font-size fontobj-2)
  431.              (font-spatial-to-canonical (font-size fontobj-2)))))
  432.     (set-font-weight retval (font-higher-weight (font-weight fontobj-1)
  433.                         (font-weight fontobj-2)))
  434.     (set-font-family retval (font-unique (append (font-family fontobj-1)
  435.                          (font-family fontobj-2))))
  436.     (set-font-style retval (| (font-style fontobj-1) (font-style fontobj-2)))
  437.     (set-font-registry retval (or (font-registry fontobj-1)
  438.                   (font-registry fontobj-2)))
  439.     (set-font-encoding retval (or (font-encoding fontobj-1)
  440.                   (font-encoding fontobj-2)))
  441.     (set-font-size retval (cond
  442.                ((and size-1 size-2 (>= size-2 size-1))
  443.                 (font-size fontobj-2))
  444.                ((and size-1 size-2)
  445.                 (font-size fontobj-1))
  446.                (size-1
  447.                 (font-size fontobj-1))
  448.                (size-2
  449.                 (font-size fontobj-2))
  450.                (t nil)))
  451.  
  452.     retval))
  453.  
  454. (defun font-combine-fonts (&rest args)
  455.   (cond
  456.    ((null args)
  457.     (error "Wrong number of arguments to font-combine-fonts"))
  458.    ((= (length args) 1)
  459.     (car args))
  460.    (t
  461.     (let ((retval (font-combine-fonts-internal (nth 0 args) (nth 1 args))))
  462.       (setq args (cdr (cdr args)))
  463.       (while args
  464.     (setq retval (font-combine-fonts-internal retval (car args))
  465.           args (cdr args)))
  466.       retval))))
  467.  
  468.  
  469. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  470. ;;; The window-system dependent code (TTY-style)
  471. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  472. (defun tty-font-create-object (fontname &optional device)
  473.   (make-font :size "12pt"))
  474.  
  475. (defun tty-font-create-plist (fontobj &optional device)
  476.   (let ((styles (font-style fontobj))
  477.     (weight (font-weight fontobj)))
  478.     (list
  479.      (cons 'underline (font-underline-p fontobj))
  480.      (cons 'highlight (if (or (font-bold-p fontobj)
  481.                   (memq weight '(:bold :demi-bold))) t))
  482.      (cons 'dim       (font-dim-p fontobj))
  483.      (cons 'blinking  (font-blink-p fontobj))
  484.      (cons 'reverse   (font-reverse-p fontobj)))))
  485.  
  486.  
  487. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  488. ;;; The window-system dependent code (X-style)
  489. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  490. (defvar font-x-font-regexp (or (and font-running-xemacs
  491.                     (boundp 'x-font-regexp)
  492.                     x-font-regexp)
  493.  (let
  494.      ((-         "[-?]")
  495.       (foundry        "[^-]*")
  496.       (family         "[^-]*")
  497.       (weight        "\\(bold\\|demibold\\|medium\\|black\\)")
  498.       (weight\?        "\\([^-]*\\)")
  499.       (slant        "\\([ior]\\)")
  500.       (slant\?        "\\([^-]?\\)")
  501.       (swidth        "\\([^-]*\\)")
  502.       (adstyle        "\\([^-]*\\)")
  503.       (pixelsize    "\\(\\*\\|[0-9]+\\)")
  504.       (pointsize    "\\(\\*\\|0\\|[0-9][0-9]+\\)")
  505.       (resx        "\\([*0]\\|[0-9][0-9]+\\)")
  506.       (resy        "\\([*0]\\|[0-9][0-9]+\\)")
  507.       (spacing        "[cmp?*]")
  508.       (avgwidth        "\\(\\*\\|[0-9]+\\)")
  509.       (registry        "[^-]*")
  510.       (encoding    "[^-]+")
  511.       )
  512.    (concat "\\`\\*?[-?*]"
  513.        foundry - family - weight\? - slant\? - swidth - adstyle -
  514.        pixelsize - pointsize - resx - resy - spacing - avgwidth -
  515.        registry - encoding "\\'"
  516.        ))))
  517.  
  518. (defvar font-x-registry-and-encoding-regexp
  519.   (or (and font-running-xemacs
  520.        (boundp 'x-font-regexp-registry-and-encoding)
  521.        (symbol-value 'x-font-regexp-registry-and-encoding))
  522.       (let ((- "[-?]")
  523.         (registry "[^-]*")
  524.         (encoding "[^-]+"))
  525.     (concat - "\\(" registry "\\)" - "\\(" encoding "\\)\\'"))))
  526.  
  527. (defun x-font-create-object (fontname &optional device)
  528.   (let ((case-fold-search t))
  529.     (if (or (not (stringp fontname))
  530.         (not (string-match font-x-font-regexp fontname)))
  531.     (make-font)
  532.       (let ((family nil)
  533.         (style nil)
  534.         (size nil)
  535.         (weight  (match-string 1 fontname))
  536.         (slant   (match-string 2 fontname))
  537.         (swidth  (match-string 3 fontname))
  538.         (adstyle (match-string 4 fontname))
  539.         (pxsize  (match-string 5 fontname))
  540.         (ptsize  (match-string 6 fontname))
  541.         (retval nil)
  542.         (case-fold-search t)
  543.         )
  544.     (if (not (string-match x-font-regexp-foundry-and-family fontname))
  545.         nil
  546.       (setq family (list (downcase (match-string 1 fontname)))))
  547.     (if (string= "*" weight)  (setq weight  nil))
  548.     (if (string= "*" slant)   (setq slant   nil))
  549.     (if (string= "*" swidth)  (setq swidth  nil))
  550.     (if (string= "*" adstyle) (setq adstyle nil))
  551.     (if (string= "*" pxsize)  (setq pxsize  nil))
  552.     (if (string= "*" ptsize)  (setq ptsize  nil))
  553.     (if ptsize (setq size (/ (string-to-int ptsize) 10)))
  554.     (if (and (not size) pxsize) (setq size (concat pxsize "px")))
  555.     (if weight (setq weight (intern-soft (concat ":" (downcase weight)))))
  556.     (if (and adstyle (not (equal adstyle "")))
  557.         (setq family (append family (list (downcase adstyle)))))
  558.     (setq retval (make-font :family family
  559.                 :weight weight
  560.                 :size size))
  561.     (set-font-bold-p retval (eq :bold weight))
  562.     (cond
  563.      ((null slant) nil)
  564.      ((member slant '("i" "I"))
  565.       (set-font-italic-p retval t))
  566.      ((member slant '("o" "O"))
  567.       (set-font-oblique-p retval t)))
  568.     (if (string-match font-x-registry-and-encoding-regexp fontname)
  569.         (progn
  570.           (set-font-registry retval (match-string 1 fontname))
  571.           (set-font-encoding retval (match-string 2 fontname))))
  572.     retval))))
  573.  
  574. (defun x-font-families-for-device (&optional device no-resetp)
  575.   (condition-case ()
  576.       (require 'x-font-menu)
  577.     (error nil))
  578.   (or device (setq device (selected-device)))
  579.   (if (boundp 'device-fonts-cache)
  580.       (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
  581.     (if (and (not menu) (not no-resetp))
  582.         (progn
  583.           (reset-device-font-menus device)
  584.           (x-font-families-for-device device t))
  585.       (let ((scaled (mapcar (function (lambda (x) (if x (aref x 0))))
  586.                 (aref menu 0)))
  587.         (normal (mapcar (function (lambda (x) (if x (aref x 0))))
  588.                 (aref menu 1))))
  589.         (sort (font-unique (nconc scaled normal)) 'string-lessp))))
  590.     (cons "monospace" (mapcar 'car font-family-mappings))))
  591.  
  592. (defvar font-default-cache nil)
  593.  
  594. ;;;###autoload
  595. (defun font-default-font-for-device (&optional device)
  596.   (or device (setq device (selected-device)))
  597.   (if font-running-xemacs
  598.       (font-truename
  599.        (make-font-specifier
  600.     (face-font-name 'default device)))
  601.     (let ((font (cdr-safe (assq 'font (frame-parameters device)))))
  602.       (if (and (fboundp 'fontsetp) (fontsetp font))
  603.       (aref (get-font-info (aref (cdr (get-fontset-info font)) 0)) 2)
  604.     font))))
  605.       
  606. ;;;###autoload
  607. (defun font-default-object-for-device (&optional device)
  608.   (let ((font (font-default-font-for-device device)))
  609.     (or (cdr-safe 
  610.      (assoc font font-default-cache))
  611.     (progn
  612.       (setq font-default-cache (cons (cons font
  613.                            (font-create-object font))
  614.                      font-default-cache))
  615.       (cdr-safe (assoc font font-default-cache))))))
  616.  
  617. ;;;###autoload
  618. (defun font-default-family-for-device (&optional device)
  619.   (or device (setq device (selected-device)))
  620.   (font-family (font-default-object-for-device device)))
  621.  
  622. ;;;###autoload
  623. (defun font-default-registry-for-device (&optional device)
  624.   (or device (setq device (selected-device)))
  625.   (font-registry (font-default-object-for-device device)))
  626.  
  627. ;;;###autoload
  628. (defun font-default-encoding-for-device (&optional device)
  629.   (or device (setq device (selected-device)))
  630.   (font-encoding (font-default-object-for-device device)))
  631.  
  632. ;;;###autoload
  633. (defun font-default-size-for-device (&optional device)
  634.   (or device (setq device (selected-device)))
  635.   ;; face-height isn't the right thing (always 1 pixel too high?)
  636.   ;; (if font-running-xemacs
  637.   ;;    (format "%dpx" (face-height 'default device))
  638.   (font-size (font-default-object-for-device device)))
  639.  
  640. (defun x-font-create-name (fontobj &optional device)
  641.   (if (and (not (or (font-family fontobj)
  642.             (font-weight fontobj)
  643.             (font-size fontobj)
  644.             (font-registry fontobj)
  645.             (font-encoding fontobj)))
  646.        (= (font-style fontobj) 0))
  647.       (face-font 'default)
  648.     (or device (setq device (selected-device)))
  649.     (let* ((default (font-default-object-for-device device))
  650.        (family (or (font-family fontobj)
  651.                (font-family default)
  652.                (x-font-families-for-device device)))
  653.        (weight (or (font-weight fontobj) :medium))
  654.        (style (font-style fontobj))
  655.        (size (or (if font-running-xemacs
  656.              (font-size fontobj))
  657.              (font-size default)))
  658.        (registry (or (font-registry fontobj)
  659.              (font-registry default)
  660.              "*"))
  661.        (encoding (or (font-encoding fontobj)
  662.              (font-encoding default)
  663.              "*")))
  664.       (if (stringp family)
  665.       (setq family (list family)))
  666.       (setq weight (font-higher-weight weight
  667.                        (and (font-bold-p fontobj) :bold)))
  668.       (if (stringp size)
  669.       (setq size (truncate (font-spatial-to-canonical size device))))
  670.       (setq weight (or (cdr-safe (assq weight x-font-weight-mappings)) "*"))
  671.       (let ((done nil)            ; Did we find a good font yet?
  672.         (font-name nil)        ; font name we are currently checking
  673.         (cur-family nil)        ; current family we are checking
  674.         )
  675.     (while (and family (not done))
  676.       (setq cur-family (car family)
  677.         family (cdr family))
  678.       (if (assoc cur-family font-family-mappings)
  679.           ;; If the family name is an alias as defined by
  680.           ;; font-family-mappings, then append those families
  681.           ;; to the front of 'family' and continue in the loop.
  682.           (setq family (append
  683.                 (cdr-safe (assoc cur-family
  684.                          font-family-mappings))
  685.                 family))
  686.         ;; Not an alias for a list of fonts, so we just check it.
  687.         ;; First, convert all '-' to spaces so that we don't screw up
  688.         ;; the oh-so wonderful X font model.  Wheee.
  689.         (let ((x (length cur-family)))
  690.           (while (> x 0)
  691.         (if (= ?- (aref cur-family (1- x)))
  692.             (aset cur-family (1- x) ? ))
  693.         (setq x (1- x))))
  694.         ;; We treat oblique and italic as equivalent.  Don't ask.
  695.         (let ((slants '("o" "i")))
  696.           (while (and slants (not done))
  697.         (setq font-name (format "-*-%s-%s-%s-*-*-*-%s-*-*-*-*-%s-%s"
  698.                     cur-family weight
  699.                     (if (or (font-italic-p fontobj)
  700.                         (font-oblique-p fontobj))
  701.                         (car slants)
  702.                       "r")
  703.                     (if size
  704.                         (int-to-string (* 10 size)) "*")
  705.                     registry
  706.                     encoding
  707.                     )
  708.               slants (cdr slants)
  709.               done (try-font-name font-name device))))))
  710.     (if done font-name)))))
  711.  
  712.  
  713. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  714. ;;; The window-system dependent code (NS-style)
  715. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  716. (defun ns-font-families-for-device (&optional device no-resetp)
  717.   ;; For right now, assume we are going to have the same storage for
  718.   ;; device fonts for NS as we do for X.  Is this a valid assumption?
  719.   (or device (setq device (selected-device)))
  720.   (if (boundp 'device-fonts-cache)
  721.       (let ((menu (or (cdr-safe (assq device device-fonts-cache)))))
  722.     (if (and (not menu) (not no-resetp))
  723.         (progn
  724.           (reset-device-font-menus device)
  725.           (ns-font-families-for-device device t))
  726.       (let ((scaled (mapcar (function (lambda (x) (if x (aref x 0))))
  727.                 (aref menu 0)))
  728.         (normal (mapcar (function (lambda (x) (if x (aref x 0))))
  729.                 (aref menu 1))))
  730.         (sort (font-unique (nconc scaled normal)) 'string-lessp))))))
  731.  
  732. (defun ns-font-create-name (fontobj &optional device)
  733.   (let ((family (or (font-family fontobj)
  734.             (ns-font-families-for-device device)))
  735.     (weight (or (font-weight fontobj) :medium))
  736.     (style (or (font-style fontobj) (list :normal)))
  737.     (size (font-size fontobj))
  738.     (registry (or (font-registry fontobj) "*"))
  739.     (encoding (or (font-encoding fontobj) "*")))
  740.     ;; Create a font, wow!
  741.     (if (stringp family)
  742.     (setq family (list family)))
  743.     (if (or (symbolp style) (numberp style))
  744.     (setq style (list style)))
  745.     (setq weight (font-higher-weight weight (car-safe (memq :bold style))))
  746.     (if (stringp size)
  747.     (setq size (font-spatial-to-canonical size device)))
  748.     (setq weight (or (cdr-safe (assq weight ns-font-weight-mappings))
  749.              "medium"))
  750.     (let ((done nil)            ; Did we find a good font yet?
  751.       (font-name nil)        ; font name we are currently checking
  752.       (cur-family nil)        ; current family we are checking
  753.       )
  754.       (while (and family (not done))
  755.     (setq cur-family (car family)
  756.           family (cdr family))
  757.     (if (assoc cur-family font-family-mappings)
  758.         ;; If the family name is an alias as defined by
  759.         ;; font-family-mappings, then append those families
  760.         ;; to the front of 'family' and continue in the loop.
  761.         (setq family (append
  762.               (cdr-safe (assoc cur-family
  763.                        font-family-mappings))
  764.               family))
  765.       ;; CARL: Need help here - I am not familiar with the NS font
  766.       ;; model
  767.       (setq font-name "UNKNOWN FORMULA GOES HERE"
  768.         done (try-font-name font-name device))))
  769.       (if done font-name))))
  770.  
  771.  
  772. ;;; Cache building code
  773. ;;;###autoload
  774. (defun x-font-build-cache (&optional device)
  775.   (let ((hashtable (make-hash-table :test 'equal :size 15))
  776.     (fonts (mapcar 'x-font-create-object
  777.                (x-list-fonts "-*-*-*-*-*-*-*-*-*-*-*-*-*-*")))
  778.     (plist nil)
  779.     (cur nil))
  780.     (while fonts
  781.       (setq cur (car fonts)
  782.         fonts (cdr fonts)
  783.         plist (cl-gethash (car (font-family cur)) hashtable))
  784.       (if (not (memq (font-weight cur) (plist-get plist 'weights)))
  785.       (setq plist (plist-put plist 'weights (cons (font-weight cur)
  786.                               (plist-get plist 'weights)))))
  787.       (if (not (member (font-size cur) (plist-get plist 'sizes)))
  788.       (setq plist (plist-put plist 'sizes (cons (font-size cur)
  789.                             (plist-get plist 'sizes)))))
  790.       (if (and (font-oblique-p cur)
  791.            (not (memq 'oblique (plist-get plist 'styles))))
  792.       (setq plist (plist-put plist 'styles (cons 'oblique (plist-get plist 'styles)))))
  793.       (if (and (font-italic-p cur)
  794.            (not (memq 'italic (plist-get plist 'styles))))
  795.       (setq plist (plist-put plist 'styles (cons 'italic (plist-get plist 'styles)))))
  796.       (cl-puthash (car (font-family cur)) plist hashtable))
  797.     hashtable))
  798.  
  799.  
  800. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  801. ;;; Now overwrite the original copy of set-face-font with our own copy that
  802. ;;; can deal with either syntax.
  803. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  804. ;;;###autoload
  805. (defun font-set-face-font (&optional face font &rest args)
  806.   (cond
  807.    ((and (vectorp font) (= (length font) 12))
  808.     (let ((font-name (font-create-name font)))
  809.       (set-face-property face 'font-specification font)
  810.       (cond
  811.        ((null font-name)        ; No matching font!
  812.     nil)
  813.        ((listp font-name)        ; For TTYs
  814.     (let (cur)
  815.       (while font-name
  816.         (setq cur (car font-name)
  817.           font-name (cdr font-name))
  818.         (apply 'set-face-property face (car cur) (cdr cur) args))))
  819.        (font-running-xemacs
  820.     (apply 'set-face-font face font-name args)
  821.     (apply 'set-face-underline-p face (font-underline-p font) args)
  822.     (if (and (or (font-smallcaps-p font) (font-bigcaps-p font))
  823.          (fboundp 'set-face-display-table))
  824.         (apply 'set-face-display-table
  825.            face font-caps-display-table args))
  826.     (apply 'set-face-property face 'strikethru (or
  827.                             (font-linethrough-p font)
  828.                             (font-strikethru-p font))
  829.            args))
  830.        (t
  831.     (condition-case nil
  832.         (apply 'set-face-font face font-name args)
  833.       (error
  834.        (let ((args (car-safe args)))
  835.          (and (or (font-bold-p font)
  836.               (memq (font-weight font) '(:bold :demi-bold)))
  837.           (make-face-bold face args t))
  838.          (and (font-italic-p font) (make-face-italic face args t)))))
  839.     (apply 'set-face-underline-p face (font-underline-p font) args)))))
  840.    (t
  841.     ;; Let the original set-face-font signal any errors
  842.     (set-face-property face 'font-specification nil)
  843.     (apply 'set-face-font face font args))))
  844.  
  845.  
  846. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  847. ;;; Now for emacsen specific stuff
  848. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  849. (defun font-update-device-fonts (device)
  850.   ;; Update all faces that were created with the 'font' package
  851.   ;; to appear correctly on the new device.  This should be in the
  852.   ;; create-device-hook.  This is XEmacs 19.12+ specific
  853.   (let ((faces (face-list 2))
  854.     (cur nil)
  855.     (font nil)
  856.     (font-spec nil))
  857.     (while faces
  858.       (setq cur (car faces)
  859.         faces (cdr faces)
  860.         font-spec (face-property cur 'font-specification))
  861.       (if font-spec
  862.       (set-face-font cur font-spec device)))))
  863.  
  864. (defun font-update-one-face (face &optional device-list)
  865.   ;; Update FACE on all devices in DEVICE-LIST
  866.   ;; DEVICE_LIST defaults to a list of all active devices
  867.   (setq device-list (or device-list (device-list)))
  868.   (if (devicep device-list)
  869.       (setq device-list (list device-list)))
  870.   (let* ((cur-device nil)
  871.      (font-spec (face-property face 'font-specification))
  872.      (font nil))
  873.     (if (not font-spec)
  874.     ;; Hey!  Don't mess with fonts we didn't create in the
  875.     ;; first place.
  876.     nil
  877.       (while device-list
  878.     (setq cur-device (car device-list)
  879.           device-list (cdr device-list))
  880.     (if (not (device-live-p cur-device))
  881.         ;; Whoah!
  882.         nil
  883.       (if font-spec
  884.           (set-face-font face font-spec cur-device)))))))
  885.  
  886. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  887. ;;; Various color related things
  888. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  889. (cond
  890.  ((fboundp 'display-warning)
  891.   (fset 'font-warn 'display-warning))
  892.  ((fboundp 'w3-warn)
  893.   (fset 'font-warn 'w3-warn))
  894.  ((fboundp 'url-warn)
  895.   (fset 'font-warn 'url-warn))
  896.  ((fboundp 'warn)
  897.   (defun font-warn (class message &optional level)
  898.     (warn "(%s/%s) %s" class (or level 'warning) message)))
  899.  (t
  900.   (defun font-warn (class message &optional level)
  901.     (save-excursion
  902.       (set-buffer (get-buffer-create "*W3-WARNINGS*"))
  903.       (goto-char (point-max))
  904.       (save-excursion
  905.     (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
  906.       (display-buffer (current-buffer))))))
  907.  
  908. (defun font-lookup-rgb-components (color)
  909.   "Lookup COLOR (a color name) in rgb.txt and return a list of RGB values.
  910. The list (R G B) is returned, or an error is signaled if the lookup fails."
  911.   (let ((lib-list (if (boundp 'x-library-search-path)
  912.               x-library-search-path
  913.             ;; This default is from XEmacs 19.13 - hope it covers
  914.             ;; everyone.
  915.             (list "/usr/X11R6/lib/X11/"
  916.               "/usr/X11R5/lib/X11/"
  917.               "/usr/lib/X11R6/X11/"
  918.               "/usr/lib/X11R5/X11/"
  919.               "/usr/local/X11R6/lib/X11/"
  920.               "/usr/local/X11R5/lib/X11/"
  921.               "/usr/local/lib/X11R6/X11/"
  922.               "/usr/local/lib/X11R5/X11/"
  923.               "/usr/X11/lib/X11/"
  924.               "/usr/lib/X11/"
  925.               "/usr/local/lib/X11/"
  926.               "/usr/X386/lib/X11/"
  927.               "/usr/x386/lib/X11/"
  928.               "/usr/XFree86/lib/X11/"
  929.               "/usr/unsupported/lib/X11/"
  930.               "/usr/athena/lib/X11/"
  931.               "/usr/local/x11r5/lib/X11/"
  932.               "/usr/lpp/Xamples/lib/X11/"
  933.               "/usr/openwin/lib/X11/"
  934.               "/usr/openwin/share/lib/X11/")))
  935.     (file font-rgb-file)
  936.     r g b)
  937.     (if (not file)
  938.     (while lib-list
  939.       (setq file (expand-file-name "rgb.txt" (car lib-list)))
  940.       (if (file-readable-p file)
  941.           (setq lib-list nil
  942.             font-rgb-file file)
  943.         (setq lib-list (cdr lib-list)
  944.           file nil))))
  945.     (if (null file)
  946.     (list 0 0 0)
  947.       (save-excursion
  948.     (set-buffer (find-file-noselect file))
  949.     (if (not (= (aref (buffer-name) 0) ? ))
  950.         (rename-buffer (generate-new-buffer-name " *rgb-tmp-buffer*")))
  951.     (save-excursion
  952.       (save-restriction
  953.         (widen)
  954.         (goto-char (point-min))
  955.         (if (re-search-forward (format "\t%s$" (regexp-quote color)) nil t)
  956.         (progn
  957.           (beginning-of-line)
  958.           (setq r (* (read (current-buffer)) 256)
  959.             g (* (read (current-buffer)) 256)
  960.             b (* (read (current-buffer)) 256)))
  961.           (font-warn 'color (format "No such color: %s" color))
  962.           (setq r 0
  963.             g 0
  964.             b 0))
  965.         (list r g b) ))))))
  966.  
  967. (defun font-hex-string-to-number (string)
  968.   "Convert STRING to an integer by parsing it as a hexadecimal number."
  969.   (let ((conv-list '((?0 . 0) (?a . 10) (?A . 10)
  970.              (?1 . 1) (?b . 11) (?B . 11)
  971.              (?2 . 2) (?c . 12) (?C . 12)
  972.              (?3 . 3) (?d . 13) (?D . 13)
  973.              (?4 . 4) (?e . 14) (?E . 14)
  974.              (?5 . 5) (?f . 15) (?F . 15)
  975.              (?6 . 6) 
  976.              (?7 . 7)
  977.              (?8 . 8)
  978.              (?9 . 9)))
  979.     (n 0)
  980.     (i 0)
  981.     (lim (length string)))
  982.     (while (< i lim)
  983.       (setq n (+ (* n 16) (or (cdr (assq (aref string i) conv-list)) 0))
  984.         i (1+ i)))
  985.     n ))
  986.  
  987. (defun font-parse-rgb-components (color)
  988.   "Parse RGB color specification and return a list of integers (R G B).
  989. #FEFEFE and rgb:fe/fe/fe style specifications are parsed."
  990.   (let ((case-fold-search t)
  991.     r g b str)
  992.   (cond ((string-match "^#[0-9a-f]+$" color)
  993.      (cond
  994.       ((= (length color) 4)
  995.        (setq r (font-hex-string-to-number (substring color 1 2))
  996.          g (font-hex-string-to-number (substring color 2 3))
  997.          b (font-hex-string-to-number (substring color 3 4))
  998.          r (* r 4096)
  999.          g (* g 4096)
  1000.          b (* b 4096)))
  1001.       ((= (length color) 7)
  1002.        (setq r (font-hex-string-to-number (substring color 1 3))
  1003.          g (font-hex-string-to-number (substring color 3 5))
  1004.          b (font-hex-string-to-number (substring color 5 7))
  1005.          r (* r 256)
  1006.          g (* g 256)
  1007.          b (* b 256)))
  1008.       ((= (length color) 10)
  1009.        (setq r (font-hex-string-to-number (substring color 1 4))
  1010.          g (font-hex-string-to-number (substring color 4 7))
  1011.          b (font-hex-string-to-number (substring color 7 10))
  1012.          r (* r 16)
  1013.          g (* g 16)
  1014.          b (* b 16)))
  1015.       ((= (length color) 13)
  1016.        (setq r (font-hex-string-to-number (substring color 1 5))
  1017.          g (font-hex-string-to-number (substring color 5 9))
  1018.          b (font-hex-string-to-number (substring color 9 13))))
  1019.       (t
  1020.        (font-warn 'color (format "Invalid RGB color specification: %s"
  1021.                      color))
  1022.        (setq r 0
  1023.          g 0
  1024.          b 0))))
  1025.     ((string-match "rgb:\\([0-9a-f]+\\)/\\([0-9a-f]+\\)/\\([0-9a-f]+\\)"
  1026.                color)
  1027.      (if (or (> (- (match-end 1) (match-beginning 1)) 4)
  1028.          (> (- (match-end 2) (match-beginning 2)) 4)
  1029.          (> (- (match-end 3) (match-beginning 3)) 4))
  1030.          (error "Invalid RGB color specification: %s" color)
  1031.        (setq str (match-string 1 color)
  1032.          r (* (font-hex-string-to-number str)
  1033.               (expt 16 (- 4 (length str))))
  1034.          str (match-string 2 color)
  1035.          g (* (font-hex-string-to-number str)
  1036.               (expt 16 (- 4 (length str))))
  1037.          str (match-string 3 color)
  1038.          b (* (font-hex-string-to-number str)
  1039.               (expt 16 (- 4 (length str)))))))
  1040.     (t
  1041.      (font-warn 'html (format "Invalid RGB color specification: %s"
  1042.                 color))
  1043.      (setq r 0
  1044.            g 0
  1045.            b 0)))
  1046.   (list r g b) ))
  1047.  
  1048. (defsubst font-rgb-color-p (obj)
  1049.   (or (and (vectorp obj)
  1050.        (= (length obj) 4)
  1051.        (eq (aref obj 0) 'rgb))))
  1052.  
  1053. (defsubst font-rgb-color-red (obj) (aref obj 1))
  1054. (defsubst font-rgb-color-green (obj) (aref obj 2))
  1055. (defsubst font-rgb-color-blue (obj) (aref obj 3))
  1056.  
  1057. (defun font-color-rgb-components (color)
  1058.   "Return the RGB components of COLOR as a list of integers (R G B).
  1059. 16-bit values are always returned.
  1060. #FEFEFE and rgb:fe/fe/fe style color specifications are parsed directly
  1061. into their components.
  1062. RGB values for color names are looked up in the rgb.txt file.
  1063. The variable x-library-search-path is use to locate the rgb.txt file."
  1064.   (let ((case-fold-search t))
  1065.     (cond
  1066.      ((and (font-rgb-color-p color) (floatp (aref color 1)))
  1067.       (list (* 65535 (aref color 0))
  1068.          (* 65535 (aref color 1))
  1069.          (* 65535 (aref color 2))))
  1070.      ((font-rgb-color-p color)
  1071.       (list (font-rgb-color-red color)
  1072.         (font-rgb-color-green color)
  1073.         (font-rgb-color-blue color)))
  1074.      ((and (vectorp color) (= 3 (length color)))
  1075.       (list (aref color 0) (aref color 1) (aref color 2)))
  1076.      ((and (listp color) (= 3 (length color)) (floatp (car color)))
  1077.       (mapcar (function (lambda (x) (* x 65535))) color))
  1078.      ((and (listp color) (= 3 (length color)))
  1079.       color)
  1080.      ((or (string-match "^#" color)
  1081.       (string-match "^rgb:" color))
  1082.       (font-parse-rgb-components color))
  1083.      ((string-match "\\([0-9.]+\\)[ \t]\\([0-9.]+\\)[ \t]\\([0-9.]+\\)"
  1084.             color)
  1085.       (let ((r (string-to-number (match-string 1 color)))
  1086.         (g (string-to-number (match-string 2 color)))
  1087.         (b (string-to-number (match-string 3 color))))
  1088.     (if (floatp r)
  1089.         (setq r (round (* 255 r))
  1090.           g (round (* 255 g))
  1091.           b (round (* 255 b))))
  1092.     (font-parse-rgb-components (format "#%02x%02x%02x" r g b))))
  1093.      (t
  1094.       (font-lookup-rgb-components color)))))
  1095.  
  1096. (defsubst font-tty-compute-color-delta (col1 col2)
  1097.   (+ 
  1098.    (* (- (aref col1 0) (aref col2 0))
  1099.       (- (aref col1 0) (aref col2 0)))
  1100.    (* (- (aref col1 1) (aref col2 1))
  1101.       (- (aref col1 1) (aref col2 1)))
  1102.    (* (- (aref col1 2) (aref col2 2))
  1103.       (- (aref col1 2) (aref col2 2)))))
  1104.  
  1105. (defun font-tty-find-closest-color (r g b)
  1106.   ;; This is basically just a lisp copy of allocate_nearest_color
  1107.   ;; from objects-x.c from Emacs 19
  1108.   ;; We really should just check tty-color-list, but unfortunately
  1109.   ;; that does not include any RGB information at all.
  1110.   ;; So for now we just hardwire in the default list and call it
  1111.   ;; good for now.
  1112.   (setq r (/ r 65535.0)
  1113.     g (/ g 65535.0)
  1114.     b (/ b 65535.0))
  1115.   (let* ((color_def (vector r g b))
  1116.      (colors [([1.0 1.0 1.0] . "white")
  1117.           ([0.0 1.0 1.0] . "cyan")
  1118.           ([1.0 0.0 1.0] . "magenta")
  1119.           ([0.0 0.0 1.0] . "blue")
  1120.           ([1.0 1.0 0.0] . "yellow")
  1121.           ([0.0 1.0 0.0] . "green")
  1122.           ([1.0 0.0 0.0] . "red")
  1123.           ([0.0 0.0 0.0] . "black")])
  1124.      (no_cells (length colors))
  1125.      (x 1)
  1126.      (nearest 0)
  1127.      (nearest_delta 0)
  1128.      (trial_delta 0))
  1129.     (setq nearest_delta (font-tty-compute-color-delta (car (aref colors 0))
  1130.                               color_def))
  1131.     (while (/= no_cells x)
  1132.       (setq trial_delta (font-tty-compute-color-delta (car (aref colors x))
  1133.                               color_def))
  1134.       (if (< trial_delta nearest_delta)
  1135.       (setq nearest x
  1136.         nearest_delta trial_delta))
  1137.       (setq x (1+ x)))
  1138.     (cdr-safe (aref colors nearest))))
  1139.  
  1140. (defun font-normalize-color (color &optional device)
  1141.   "Return an RGB tuple, given any form of input.  If an error occurs, black
  1142. is returned."
  1143.   (case (device-type device)
  1144.    ((x pm)
  1145.     (apply 'format "#%02x%02x%02x" (font-color-rgb-components color)))
  1146.    (win32
  1147.     (let* ((rgb (font-color-rgb-components color))
  1148.        (color (apply 'format "#%02x%02x%02x" rgb)))
  1149.       (win32-define-rgb-color (nth 0 rgb) (nth 1 rgb) (nth 2 rgb) color)
  1150.       color))
  1151.    (w32
  1152.     (let* ((rgb (font-color-rgb-components color))
  1153.        (color (apply 'format "#%02x%02x%02x" rgb)))
  1154.       (w32-define-rgb-color (nth 0 rgb) (nth 1 rgb) (nth 2 rgb) color)
  1155.       color))
  1156.    (tty
  1157.     (apply 'font-tty-find-closest-color (font-color-rgb-components color)))
  1158.    (ns
  1159.     (let ((vals (mapcar (function (lambda (x) (>> x 8)))
  1160.             (font-color-rgb-components color))))
  1161.       (apply 'format "RGB%02x%02x%02xff" vals)))
  1162.    (otherwise
  1163.     color)))
  1164.  
  1165. (defun font-set-face-background (&optional face color &rest args)
  1166.   (interactive)
  1167.   (condition-case nil
  1168.       (cond
  1169.        ((or (font-rgb-color-p color)
  1170.         (string-match "^#[0-9a-fA-F]+$" color))
  1171.     (apply 'set-face-background face
  1172.            (font-normalize-color color) args))
  1173.        (t
  1174.     (apply 'set-face-background face color args)))
  1175.     (error nil)))
  1176.  
  1177. (defun font-set-face-foreground (&optional face color &rest args)
  1178.   (interactive)
  1179.   (condition-case nil
  1180.       (cond
  1181.        ((or (font-rgb-color-p color)
  1182.         (string-match "^#[0-9a-fA-F]+$" color))
  1183.     (apply 'set-face-foreground face (font-normalize-color color) args))
  1184.        (t
  1185.     (apply 'set-face-foreground face color args)))
  1186.     (error nil)))
  1187.  
  1188. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1189. ;;; Support for 'blinking' fonts
  1190. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1191. (defun font-map-windows (func &optional arg frame)
  1192.   (let* ((start (selected-window))
  1193.      (cur start)
  1194.      (result nil))
  1195.     (push (funcall func start arg) result)
  1196.     (while (not (eq start (setq cur (next-window cur))))
  1197.       (push (funcall func cur arg) result))
  1198.     result))
  1199.  
  1200. (defun font-face-visible-in-window-p (window face)
  1201.   (let ((st (window-start window))
  1202.     (nd (window-end window))
  1203.     (found nil)
  1204.     (face-at nil))
  1205.     (setq face-at (get-text-property st 'face (window-buffer window)))
  1206.     (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
  1207.     (setq found t))
  1208.     (while (and (not found)
  1209.         (/= nd
  1210.             (setq st (next-single-property-change
  1211.                   st 'face
  1212.                   (window-buffer window) nd))))
  1213.       (setq face-at (get-text-property st 'face (window-buffer window)))
  1214.       (if (or (eq face face-at) (and (listp face-at) (memq face face-at)))
  1215.       (setq found t)))
  1216.     found))
  1217.   
  1218. (defun font-blink-callback ()
  1219.   ;; Optimized to never invert the face unless one of the visible windows
  1220.   ;; is showing it.
  1221.   (let ((faces (if font-running-xemacs (face-list t) (face-list)))
  1222.     (obj nil))
  1223.     (while faces
  1224.       (if (and (setq obj (face-property (car faces) 'font-specification))
  1225.            (font-blink-p obj)
  1226.            (memq t
  1227.              (font-map-windows 'font-face-visible-in-window-p (car faces))))
  1228.       (invert-face (car faces)))
  1229.       (pop faces))))
  1230.  
  1231. (defcustom font-blink-interval 0.5
  1232.   "How often to blink faces"
  1233.   :type 'number
  1234.   :group 'faces)
  1235.   
  1236. (defun font-blink-initialize ()
  1237.   (cond
  1238.    ((featurep 'itimer)
  1239.     (if (get-itimer "font-blinker")
  1240.     (delete-itimer (get-itimer "font-blinker")))
  1241.     (start-itimer "font-blinker" 'font-blink-callback
  1242.           font-blink-interval
  1243.           font-blink-interval))
  1244.    ((fboundp 'run-at-time)
  1245.     (cancel-function-timers 'font-blink-callback)    
  1246.     (run-at-time font-blink-interval
  1247.          font-blink-interval
  1248.          'font-blink-callback))
  1249.    (t nil)))
  1250.   
  1251. (provide 'font)
  1252.